
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.Charset;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

/*
 * This is ridiculously simple, and mostly just slapped together in a few hours of playing around
 * Might not be the best to learn off of, but it's here.
 */

/**
 *
 * @author Zack
 */
public class SocketTest {
    static ServerSocket server;
    static Socket client;
    static BufferedReader is;
    static PrintWriter os;
    static boolean isServer;
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        JFrame frame = new JFrame("Stuff");
        //MainMenu main = new MainMenu(frame);
        JPanel panel = new JPanel();
        panel.setFocusable(true);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        TextArea text = new TextArea("Never tell your password to anyone.\n", 40, 50, TextArea.SCROLLBARS_VERTICAL_ONLY);
        TextField input = new TextField(50);
        input.addKeyListener(new keyL(text, input));
        text.setEditable(false);
        panel.add(text);
        panel.add(input);
        frame.add(panel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.pack();
        isServer = false;
        
        String line;
        
        if(args.length > 0){
            if(args[0].equals("-c")){
                try{
                    client = new Socket("128.61.105.7", 4221);
                    //client = new Socket("127.0.0.1", 4221);
                    os = new PrintWriter(client.getOutputStream(), true);
                    is = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    System.out.println("Client streams ready");
                     while(true){
                        line = is.readLine();
                        text.append("[Server]: " + line + "\n");
                    }
                }catch(Throwable t){
                    System.out.println(t.getMessage());
                }
                
                   
            }else{
                try{
                    server = new ServerSocket(4221);
                    isServer = true;
                }catch(Throwable t){
                    System.out.println(t.getMessage());
                }
                
                try{
                    client = server.accept();
                    is = new BufferedReader(new InputStreamReader(client.getInputStream()));
                    os = new PrintWriter(client.getOutputStream(), true);
                    System.out.println("Server streams ready");
                    while(true){
                        line = is.readLine();
                        text.append("[Client]: " + line + "\n");
                    }
                }catch(Throwable t){
                    System.out.println(t.getMessage());
                }
            }
        }
        // TODO code application logic here
    }
    private static class keyL implements KeyListener{
        TextArea area;
        TextField input;
        public keyL(TextArea area, TextField input){
            this.area = area;
            this.input = input;
        }
        
        @Override
        public void keyTyped(KeyEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void keyPressed(KeyEvent e) {
            if(e.getKeyCode() == 10){
                area.append((isServer ? "[Server]: " : "[Client]: ") + input.getText() + "\n");
                if(os != null){
                    os.println(input.getText());
                }else{
                    System.out.println("Output Stream not yet ready");
                }
                input.setText("");
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
        
    }
}
